﻿using RilattackLibrary;
using System;
using System.ServiceModel;
using System.Threading;

namespace RilattackGameWPF
{
	[ServiceBehavior(
	InstanceContextMode = InstanceContextMode.PerSession,
	ConcurrencyMode = ConcurrencyMode.Reentrant)]
	public class CallBack : ICallBack
    {
        public Scene Scene
        {
            get; set;
        }

        // initialisé pendant le login
        public ControlablePlayer CurrentPlayer
        {
            get; set;
        }

        public void UpdateScene()
        {
            throw new System.NotImplementedException();
        }

        public void beginNewGame(GameConfiguration configuration)
        {
            // if closed, we loose callback ability, so just hide it
            Thread gameThread = new Thread(App.LaunchGame);

            App.Window.Hide();
            gameThread.Start();
            Scene.Instance.GameConfiguration = configuration;
		}

        public void InitPlayer(Guid playerUid, string pseudo, int posX, int posY)
        {
			Console.WriteLine("Called Init player for " + pseudo + " this method should only called once for each players.");
            Player p;

            if (playerUid == CurrentPlayer.UID && CurrentPlayer != null)
            {
                p = CurrentPlayer;
            }
            else
            {
                p = new Player();
                p.Nickname = pseudo;
            }
            p.SetRect(posX, posY, Player.playerW, Player.playerH);

			Scene.Instance.AddNewPlayer(p);
		}

		public void MovePlayer(String playerNick, int posX, int posY)
        {
            if (Scene.Instance != null && Scene.Instance.Actors != null)
                lock (Scene.Instance.Actors)
                {
                    if (!Scene.Instance.SetPlayerPos(playerNick, posX, posY))
                    {
                        //Console.WriteLine("WARN - Server requested update on an unknown player or client did not respond.");
                    }
                }
		}
    }
}